<template>
  <div class="app-container">
    <el-form size="large"  :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="阶段名称" prop="phaseName">
        <el-input v-model="queryParams.phaseName" placeholder="请输入阶段名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
         >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
         >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dictionaryList" @selection-change="handleSelectionChange">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="阶段名称" show-overflow-tooltip width="200"  align="center" prop="phaseName" />
      <el-table-column label="阶段描述" show-overflow-tooltip width="200"  align="center" prop="phaseDesc" />
      
      <el-table-column label="该环节需要的文件类型" show-overflow-tooltip width="200"  align="center" prop="needFileType">
        <template #default="scope">
          <span v-if="Array.isArray(scope.row.needFileType)">
      {{
        scope.row.needFileType
          .map(value => file_type.find(item => item.value == value)?.label || value)
          .join('、')
      }}
    </span>
        </template>

      </el-table-column>
      <el-table-column label="该阶段需要的参与人" show-overflow-tooltip width="200"  align="center" prop="needPersonId">
        <template #default="scope">
          <dict-tag :options="is_need" :value="scope.row.needPersonId" />
        </template>
      </el-table-column>
      
      <el-table-column label="该阶段是否需要付款信息" show-overflow-tooltip width="200"  align="center" prop="needPay">
        <template #default="scope">
          <dict-tag :options="is_need" :value="scope.row.needPay" />
        </template>
      </el-table-column>
      <el-table-column label="是否需要实施工作日志" show-overflow-tooltip width="200"  align="center" prop="needImplement">
        <template #default="scope">
          <dict-tag :options="is_need" :value="scope.row.needImplement" />
        </template>
      </el-table-column>
      <el-table-column label="是否需要质保信息"  show-overflow-tooltip width="200"  align="center" prop="needWarranty">
        <template #default="scope">
          <dict-tag :options="is_need" :value="scope.row.needWarranty" />
        </template>
      </el-table-column>
      <el-table-column label="是否需要添加核算信息" show-overflow-tooltip width="200"  align="center" prop="needAccounting">
        <template #default="scope">
          <dict-tag :options="is_need" :value="scope.row.needAccounting" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="排序" align="center" prop="soft" />
      <el-table-column label="操作人id" align="center" prop="operatorId" />
      <el-table-column label="操作人姓名" align="center" prop="personName" />
      <el-table-column label="操作" align="center" fixed="right" width="220" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
         >修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['project:dictionary:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改阶段字典配置对话框 -->
    <el-dialog :title="title" fullscreen v-model="open" width="500px" append-to-body>
      <el-form size="large"  ref="dictionaryRef" :model="form" :rules="rules" label-width="220px">
        <el-form-item label="阶段名称" prop="phaseName">
          <el-input v-model="form.phaseName" placeholder="请输入阶段名称" />
        </el-form-item>
        
        <el-form-item label="排序" prop="soft">
          <el-input v-model="form.soft" placeholder="请输入排序" />
        </el-form-item>
        
        <el-form-item label="阶段描述" prop="phaseDesc">
          <el-input v-model="form.phaseDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="该阶段是否需要付款信息" prop="needPay">
          <el-radio-group v-model="form.needPay">
            <el-radio v-for="dict in is_need" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否需要实施工作日志" prop="needImplement">
          <el-radio-group v-model="form.needImplement">
            <el-radio v-for="dict in is_need" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否需要质保信息" prop="needWarranty">
          <el-radio-group v-model="form.needWarranty">
            <el-radio v-for="dict in is_need" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        
        <el-form-item label="是否需要添加核算信息" prop="needAccounting">
          <el-radio-group v-model="form.needAccounting">
            <el-radio v-for="dict in is_need" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="该环节需要的文件类型" prop="needFileType">
          <el-select v-model="form.needFileType" multiple placeholder="请选择该环节需要的文件类型">
            <el-option v-for="dict in file_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="文件是否全量" prop="allFile">
          <el-radio-group v-model="form.allFile">
            <el-radio v-for="dict in is_need" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
          <el-tag type="primary" size="large" style="margin-left: 10px;">提示：选择是则代表选择的文件必须全部上传，否则上传其中之一即可</el-tag>
        </el-form-item>

        <!--  -->

        <el-form-item label="指定操作人" prop="operatorId">
          <el-select    style="width: 500px;" @change="setPersonName" multiple filterable v-model="form.operatorId" placeholder="请选择指定的操作人"
            clearable >
            <el-option v-for="dict in userList" clearable :key="dict.userId" :label="dict.nickName"
              :value="dict.userId"></el-option>
          </el-select>
          <el-tag type="primary" size="large" style="margin-left: 10px;">提示：指定操作人后该环节只能由指定的操作人处理</el-tag>
        </el-form-item>

        <el-form-item label="是否需要全部操作人同意" prop="needOperator">
          <el-radio-group v-model="form.needOperator">
            <el-radio v-for="dict in is_need" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
          <el-tag type="primary" size="large" style="margin-left: 10px;">提示：选择是则代表需要指定的人全部同意才可流转到下一步</el-tag>
        </el-form-item>



        <el-form-item label="该环节需要的参与人岗位" prop="needPersonId">
          <el-select  filterable multiple v-model="form.needPersonId" placeholder="请选择岗位" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="当前阶段操作人" prop="postId">
          <el-select  filterable multiple v-model="form.postId" placeholder="请选择岗位" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item>

        
        <el-form-item label="是否必须全部操作人同意" prop="allPerson">
          <el-radio-group v-model="form.allPerson">
            <el-radio v-for="dict in is_need" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
          <el-tag type="primary" size="large" style="margin-left: 10px;">提示：选择是则代表该阶段需要选取的全部操作人都点击下一步后才能提交到下一步</el-tag>
        </el-form-item>


        <el-form-item label="添加参与人员权限" prop="addPerson">
          <el-select  filterable multiple v-model="form.addPerson" placeholder="请选择" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item>

        

        <el-form-item label="添加附件权限" prop="addFile">
          <el-select  filterable multiple v-model="form.addFile" placeholder="请选择" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="添加付款信息权限" prop="addPay">
          <el-select  filterable multiple v-model="form.addPay" placeholder="请选择" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="添加实施日志权限" prop="addImplement">
          <el-select  filterable multiple v-model="form.addImplement" placeholder="请选择" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="添加核算信息权限" prop="addAccounting">
          <el-select filterable multiple v-model="form.addAccounting" placeholder="请选择" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="添加质保信息权限" prop="addWarranty">
          <el-select  filterable multiple v-model="form.addWarranty" placeholder="请选择" clearable>
            <el-option v-for="dict in posts" clearable :key="dict.postId" :label="dict.postName"
              :value="dict.postId"></el-option>
          </el-select>
        </el-form-item> 

        <!--  -->
  
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dictionary">
import { listDictionary, getDictionary, delDictionary, addDictionary, updateDictionary } from "@/api/project/dictionary";
import { listAllUser, getUser } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const { file_type, is_need } = proxy.useDict('file_type', 'is_need');

const dictionaryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const posts = ref([])
const userList = ref([])
const data = reactive({
  form: {
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    phaseName: null,
    phaseDesc: null,
    isNeed: null,
    allFile: null,
    needPay: null,
    allPerson: null,
    needImplement: null,
    needWarranty: null,
    loginIp: null,
    loginDate: null,
    soft: null,
    operatorId: [],
    personName: null,
    needFileType: [],
    needPersonId: [],
    postId:[],
    addPerson:[],
    needAccounting: null
  },
  rules: {
    phaseName: [
      { required: true, message: "阶段名称不能为空", trigger: "blur" }
    ],
    soft: [
      { required: true, message: "排序不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询阶段字典配置列表 */
function getList() {
  loading.value = true;
  listDictionary(queryParams.value).then(response => {
    dictionaryList.value = response.data.rows;
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    phaseName: null,
    phaseDesc: null,
    isNeed: null,
    allFile: null,
    needPay: null,
    allPerson: null,
    needImplement: null,
    needWarranty: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    delFlag: null,
    loginIp: null,
    loginDate: null,
    soft: null,
    operatorId: [],//指定的操作人
    personName: null,
    needFileType: [],
    needPersonId: [],
    postId:[],
    addPerson:[],
    needAccounting: null
  };
  proxy.resetForm("dictionaryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加阶段字典配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id !== undefined && row.id !== null ? row.id : ids.value;
  console.log("修改。。。",_id,row.id);
  getDictionary(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改阶段字典配置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dictionaryRef"].validate(valid => {
    if (valid) {


      if (form.value.id != null) {

              //必须指定点击下一步的人 
      // if((form.value.operatorId?.length == 0 && form.value.postId?.llen ) && form.value.id != 1){
      //   proxy.$modal.msgError("必须指定操作人或者添加当前阶段操作人",form.value.operatorId,form.value.postId,form.value.id );
      //   retrun
      // }
        
        updateDictionary(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
                      //必须指定点击下一步的人 
      // if(form.value.operatorId || form.value.postId){
      //   proxy.$modal.msgError("必须指定操作人或者添加当前阶段操作人");
      //   retrun
      // }
        addDictionary(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除阶段字典配置编号为"' + _ids + '"的数据项？').then(function () {
    return delDictionary(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('project/dictionary/export', {
    ...queryParams.value
  }, `dictionary_${new Date().getTime()}.xlsx`)
}


function getInIt() {
  getUser().then(response => {
    posts.value = response.posts;
    console.log("岗位1111", response);
  });
  listAllUser().then(res => {
    console.log("userlist", res);
    userList.value = res
  });

}
getList();

getInIt()
</script>
